package cn.com.surker.common;

import cn.com.surker.spring.RestResultDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author lucky
 * @create 16:20
 *
 */
@ControllerAdvice
@Slf4j
public class ExceptionController{

    @ExceptionHandler(value = NullPointerException.class)
    public RestResultDto javaNullExceptionHandler(HttpServletRequest request, Throwable ex) {//APIResponse是项目中对外统一的出口封装，可以根据自身项目的需求做相应更改
        log.error("捕获到NullPointerException异常",ex);
        //异常日志入库
        return RestResultDto.fail(500 , "请求发生了空指针异常，请稍后再试: " + ex.getMessage());
    }

    @ExceptionHandler(value = EmptyResultDataAccessException.class)
    public RestResultDto javaEmptyResultDataAccessExceptionHandler(Throwable ex) {
        log.error("捕获到EmptyResultDataAccessException异常",ex);
        //异常日志入库
        return RestResultDto.fail(500 , "连接ERP系统异常，请稍后再试: " + ex.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public RestResultDto handleBindException(MethodArgumentNotValidException exception) {
        BindingResult result = exception.getBindingResult();
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            errors.forEach(p -> {
                FieldError fieldError = (FieldError) p;
                log.warn("Data check failure : object={}, field={}, errorMessage={}", fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage());
            });
            return RestResultDto.fail(result.getFieldError() == null ? "请求参数有误" : result.getFieldError().getDefaultMessage());
        }
        //其他错误
        return RestResultDto.fail("异常错误!");
    }

    /**
     * 系统异常捕获处理
     */
    @ExceptionHandler(value = Exception.class)
    public RestResultDto javaExceptionHandler(HttpServletRequest request, Throwable ex) {//APIResponse是项目中对外统一的出口封装，可以根据自身项目的需求做相应更改
        log.error("捕获到Exception异常",ex);
        RestResultDto reDto = new RestResultDto(100001,"系统发生错误,请稍后再试!");
        //异常日志入库
        return reDto;
    }

    /**
     * 自定义异常捕获处理
     */
    @ExceptionHandler(value = MessageCenterException.class)//MessageCenterException是自定义的一个异常
    public RestResultDto messageCenterExceptionHandler(MessageCenterException ex) {
        log.error("捕获到MessageCenterException异常",ex.getException());
        RestResultDto reDto = new RestResultDto(100001,"系统发生错误,请稍后再试!");
        reDto.setMsg(ex.getMessage());
        return reDto;
    }

}